package com.auto.code.util;

import com.github.pagehelper.Page;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

public class ObjectClone {
    public static <T> List<T> cloneList(List<?> sourceList, Class<T> targetClazz){
        List<T> targetList = new ArrayList<T>();
        try {
            if(sourceList instanceof Page){
                Page page = (Page) sourceList;
                List<T> tempList = new ArrayList<>();
                for(Object item : sourceList){
                    T target = targetClazz.newInstance();
                    BeanUtils.copyProperties(item, target);
                    tempList.add(target);
                }
                page.clear();
                page.addAll(tempList);
                return page;
            }else{
                for(Object item : sourceList){
                    T target = targetClazz.newInstance();
                    BeanUtils.copyProperties(item, target);
                    targetList.add(target);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return targetList;
    }
}
